%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#define _GNU_SOURCE
#include <getopt.h>

#include "parse_jobstream.h"
#include "utils.h"
#include "jobstream.h"

#ifdef DEBUG
#define dbg_flag 1
#else
#define dbg_flag 0
#endif

extern FILE *yyin;
extern int yylex();

YYSTYPE yylval;
int yyerror (char const *str);

#ifdef TESTPARSE
#define PARSE_JOBSTREAM_VERSION		"TWS 8.5 Job Stream File Parser - Version 1.0 (May 25, 2011)"
#define PARSE_JOBSTREAM_COPYRIGHT	"Copyright (C) 2011-2015 Bhaskar Bhaumik, TATA Consultancy Services Limited."
#define DEFAULT_CONTACT			"Bhaskar Bhaumik (mailto:Bhaskar_Bhaumik@homedepot.com)"
#define STD_INPUT			"-"

void version(void);
void usage(void);
#endif

TWSJob this_job;
int print_format;
char *format;
char *defaultws;
char *tablename;
char *output_file;
char *error_file;
char *log_file;
FILE *logFile;
FILE *errFile;
FILE *outFile;

%}

%union
{
	int token;
	char *string;
}

%token <string> TOK_STRING
%token <string> TOK_QSTRING
%token <token>  TOK_EXCLAIM
%token <token>  TOK_POUND
%token <token>  TOK_PLUS
%token <token>  TOK_MINUS
%token <token>  TOK_SATURDAY
%token <token>  TOK_SUNDAY
%token <token>  TOK_COLON
%token <token>  TOK_AT
%token <token>  TOK_CF
%token <token>  TOK_CONFIRMED
%token <token>  TOK_CRITICAL
%token <token>  TOK_DAYS
%token <token>  TOK_DEADLINE
%token <token>  TOK_DESC
%token <token>  TOK_DRAFT
%token <token>  TOK_END
%token <token>  TOK_EVERY
%token <token>  TOK_EXCEPT
%token <token>  TOK_FDIGNORE
%token <token>  TOK_FDNEXT
%token <token>  TOK_FDPREV
%token <token>  TOK_FOLLOWS
%token <token>  TOK_FREEDAYS
%token <token>  TOK_FROM
%token <token>  TOK_GO
%token <token>  TOK_HI
%token <token>  TOK_KEYJOB
%token <token>  TOK_KEYSCHED
%token <token>  TOK_LIMIT
%token <token>  TOK_MATCHING
%token <token>  TOK_NEEDS
%token <token>  TOK_ON
%token <token>  TOK_ONUNTIL
%token <token>  TOK_OPENS
%token <token>  TOK_PREVIOUS
%token <token>  TOK_PRIORITY
%token <token>  TOK_PROMPT
%token <token>  TOK_RELATIVE
%token <token>  TOK_RUNCYCLE
%token <token>  TOK_SAMEDAY
%token <token>  TOK_SCHEDTIME
%token <token>  TOK_SCHEDULE
%token <token>  TOK_TZ
%token <token>  TOK_TO
%token <token>  TOK_UNTIL
%token <token>  TOK_VALIDFROM
%token <token>  TOK_VALIDTO
%token <token>  TOK_VARTABLE

%type <string> sched
%%
sched : TOK_SCHEDULE TOK_COLON TOK_END {}
%%

int yyerror (char const *str)
{
	static char ts[21];
	time_t t = time((time_t *)NULL);
	struct tm *tt = localtime(&t);
	strftime(ts, 20, "%Y-%m-%d %H.%M.%S", tt);
	fprintf (errFile, "parse_job: error: %s: %s: Token[%s]\n", ts, str, yylval.string);
	return 0;
}

void init_job()
{
    this_job.option = 0;
    this_job.workstation = calloc((size_t)MAX_LEN_WS, sizeof(char));
    this_job.jobname = calloc((size_t)MAX_LEN_JN, sizeof(char));
    this_job.jobfile = calloc((size_t)MAX_LEN_JF, sizeof(char));
    this_job.streamlogon = calloc((size_t)MAX_LEN_SL, sizeof(char));
    this_job.description = calloc((size_t)MAX_LEN_DE, sizeof(char));
    this_job.tasktype = calloc((size_t)MAX_LEN_TT, sizeof(char));
    this_job.interactive = 'N';
    this_job.rccondsucc = calloc((size_t)MAX_LEN_RC, sizeof(char));
    this_job.recovery.action = calloc((size_t)MAX_LEN_REC_AC, sizeof(char));
    this_job.recovery.after_workstation = calloc((size_t)MAX_LEN_REC_WS, sizeof(char));
    this_job.recovery.after_jobname = calloc((size_t)MAX_LEN_REC_JN, sizeof(char));
    this_job.recovery.abendprompt = calloc((size_t)MAX_LEN_REC_AP, sizeof(char));
    return;
}

void reset_job()
{
    this_job.option = 0;
    this_job.workstation[0] = '\0';
    this_job.jobname[0] = '\0';
    this_job.jobfiletype = 'D';
    this_job.jobfile[0] = '\0';
    this_job.streamlogon[0] = '\0';
    this_job.description[0] = '\0';
    this_job.tasktype[0] = '\0';
    this_job.interactive = 'N';
    this_job.rccondsucc[0] = '\0';
    this_job.recovery.action[0] = '\0';
    this_job.recovery.after_workstation[0] = '\0';
    this_job.recovery.after_jobname[0] = '\0';
    this_job.recovery.abendprompt[0] = '\0';
    return;
}

void destroy_job()
{
    free((void *)this_job.workstation);
    free((void *)this_job.jobname);
    free((void *)this_job.jobfile);
    free((void *)this_job.streamlogon);
    free((void *)this_job.description);
    free((void *)this_job.tasktype);
    free((void *)this_job.rccondsucc);
    free((void *)this_job.recovery.action);
    free((void *)this_job.recovery.after_workstation);
    free((void *)this_job.recovery.after_jobname);
    free((void *)this_job.recovery.abendprompt);
    return;
}

void print_job()
{
    switch(print_format) {
        case PRINT_FMT_TXT1: /* TWS Composer Dump Format */
            if(this_job.option & OPT_FLG_WS) fprintf(outFile, "%s#", this_job.workstation);
            fprintf(outFile, "%s\n", this_job.jobname);
            fprintf(outFile, " %s %s\n", this_job.jobfiletype == 'D'? "DOCOMMAND": "SCRIPTNAME", this_job.jobfile);
            fprintf(outFile, " STREAMLOGON %s\n", this_job.streamlogon);
            if(this_job.option & OPT_FLG_DE) fprintf(outFile, " DESCRIPTION %s\n", this_job.description);
            if(this_job.option & OPT_FLG_TT) fprintf(outFile, " TASKTYPE %s\n", this_job.tasktype);
            if(this_job.interactive == 'Y') fprintf(outFile, " INTERACTIVE\n");
            if(this_job.option & OPT_FLG_RC) fprintf(outFile, " RCCONDSUCC %s\n", this_job.rccondsucc);
            if(this_job.option & OPT_FLG_REC) {
                fprintf(outFile, " RECOVERY %s\n", this_job.recovery.action);
                if(this_job.option & OPT_FLG_REC_JN) {
                    fprintf(outFile, "  AFTER ");
                    if(this_job.option & OPT_FLG_REC_WS) fprintf(outFile, "%s#", this_job.recovery.after_workstation);
                    fprintf(outFile, "%s\n", this_job.recovery.after_jobname);
                }
                if(this_job.option & OPT_FLG_REC_AP) fprintf(outFile, "  ABENDPROMPT %s\n", this_job.recovery.abendprompt);
            }
            break;
        case PRINT_FMT_TXT2: /* LDIF-like Text Output Format */
            if(this_job.option & OPT_FLG_WS) fprintf(outFile, "%-27s: %s\n", "WorkStation", this_job.workstation);
            fprintf(outFile, "%-27s: %s\n", "JobName", this_job.jobname);
            fprintf(outFile, "%-27s: %s\n", this_job.jobfiletype == 'D'? "DoCommand": "ScriptName", this_job.jobfile);
            fprintf(outFile, "%-27s: %s\n", "StreamLogon", this_job.streamlogon);
            if(this_job.option & OPT_FLG_DE) fprintf(outFile, "%-27s: %s\n", "Description", this_job.description);
            if(this_job.option & OPT_FLG_TT) fprintf(outFile, "%-27s: %s\n", "TaskType", this_job.tasktype);
            if(this_job.interactive == 'Y') fprintf(outFile, "%-27s: %s\n", "Interactive", "Yes");
            if(this_job.option & OPT_FLG_RC) fprintf(outFile, "%-27s: %s\n", "RCCondSucc", this_job.rccondsucc);
            if(this_job.option & OPT_FLG_REC) {
                fprintf(outFile, "%-27s: %s\n", "Recovery.Action", this_job.recovery.action);
                if(this_job.option & OPT_FLG_REC_WS) fprintf(outFile, "%-27s: %s\n", "Recovery.After.WorkStation", this_job.recovery.after_workstation);
                if(this_job.option & OPT_FLG_REC_JN) fprintf(outFile, "%-27s: %s\n", "Recovery.After.JobName", this_job.recovery.after_jobname);
                if(this_job.option & OPT_FLG_REC_AP) fprintf(outFile, "%-27s: %s\n", "Recovery.AbendPrompt", this_job.recovery.abendprompt);
            }
            break;
        case PRINT_FMT_TXT3: /* Single Line Text Output Format */
            if(this_job.option & OPT_FLG_WS) fprintf(outFile, "%s#", this_job.workstation);
            fprintf(outFile, "%s", this_job.jobname);
            fprintf(outFile, " %s %s", this_job.jobfiletype == 'D'? "DOCOMMAND": "SCRIPTNAME", this_job.jobfile);
            fprintf(outFile, " STREAMLOGON %s", this_job.streamlogon);
            if(this_job.option & OPT_FLG_DE) fprintf(outFile, " DESCRIPTION %s", this_job.description);
            if(this_job.option & OPT_FLG_TT) fprintf(outFile, " TASKTYPE %s", this_job.tasktype);
            if(this_job.interactive == 'Y') fprintf(outFile, " INTERACTIVE");
            if(this_job.option & OPT_FLG_RC) fprintf(outFile, " RCCONDSUCC %s", this_job.rccondsucc);
            if(this_job.option & OPT_FLG_REC) {
                fprintf(outFile, " RECOVERY %s", this_job.recovery.action);
                if(this_job.option & OPT_FLG_REC_JN) {
                    fprintf(outFile, " AFTER ");
                    if(this_job.option & OPT_FLG_REC_WS) fprintf(outFile, "%s#", this_job.recovery.after_workstation);
                    fprintf(outFile, "%s", this_job.recovery.after_jobname);
                }
                if(this_job.option & OPT_FLG_REC_AP) fprintf(outFile, " ABENDPROMPT %s", this_job.recovery.abendprompt);
            }
            fprintf(outFile, "\n");
            break;
        case PRINT_FMT_SQL1: /* INSERT SQL Statements */
            fprintf(outFile, "insert into %s (workstation, jobname, docommand, jobtype, streamlogon", tablename);
            if(this_job.option & OPT_FLG_DE) fprintf(outFile, ", description");
            if(this_job.option & OPT_FLG_TT) fprintf(outFile, ", tasktype");
            fprintf(outFile, ", interactive");
            if(this_job.option & OPT_FLG_RC) fprintf(outFile, ", rccondsucc");
            if(this_job.option & OPT_FLG_REC) {
                fprintf(outFile, ", rec_action");
                if(this_job.option & OPT_FLG_REC_JN) {
                    if(this_job.option & OPT_FLG_REC_WS) fprintf(outFile, ", rec_after_ws");
                    fprintf(outFile, ", rec_after_jn");
                }
                if(this_job.option & OPT_FLG_REC_AP) fprintf(outFile, ", rec_abnd_prm");
            }
            fprintf(outFile, ") values (");
            fprintf(outFile, "'%s', '%s', '%s', '%c', '%s'", (this_job.option & OPT_FLG_WS)? escape_sql_string(this_job.workstation): defaultws,
                                                   escape_sql_string(this_job.jobname),
                                                   escape_sql_string(this_job.jobfile),
                                                   this_job.jobfiletype,
                                                   escape_sql_string(this_job.streamlogon));
            if(this_job.option & OPT_FLG_DE) fprintf(outFile, ", '%s'", escape_sql_string(this_job.description));
            if(this_job.option & OPT_FLG_TT) fprintf(outFile, ", '%s'", escape_sql_string(this_job.tasktype));
            fprintf(outFile, ", '%c'", this_job.interactive);
            if(this_job.option & OPT_FLG_RC) fprintf(outFile, ", '%s'", escape_sql_string(this_job.rccondsucc));
            if(this_job.option & OPT_FLG_REC) {
                fprintf(outFile, ", '%s'", escape_sql_string(this_job.recovery.action));
                if(this_job.option & OPT_FLG_REC_JN) {
                    if(this_job.option & OPT_FLG_REC_WS) fprintf(outFile, ", '%s'", escape_sql_string(this_job.recovery.after_workstation));
                    fprintf(outFile, ", '%s'", escape_sql_string(this_job.recovery.after_jobname));
                }
                if(this_job.option & OPT_FLG_REC_AP) fprintf(outFile, ", '%s'", escape_sql_string(this_job.recovery.abendprompt));
            }
            fprintf(outFile, ");\n");
            break;
        case PRINT_FMT_SQL2: /* UPDATE SQL Statements */
            fprintf(outFile, "update %s set docommand = '%s', jobtype = '%c', streamlogon = '%s'", tablename, escape_sql_string(this_job.jobfile), this_job.jobfiletype, escape_sql_string(this_job.streamlogon));
            if(this_job.option & OPT_FLG_DE) fprintf(outFile, ", description = '%s'", escape_sql_string(this_job.description));
            if(this_job.option & OPT_FLG_TT) fprintf(outFile, ", tasktype = '%s'", escape_sql_string(this_job.tasktype));
            fprintf(outFile, ", interactive = '%c'", this_job.interactive);
            if(this_job.option & OPT_FLG_RC) fprintf(outFile, ", rccondsucc = '%s'", escape_sql_string(this_job.rccondsucc));
            if(this_job.option & OPT_FLG_REC) {
                fprintf(outFile, ", rec_action = '%s'", escape_sql_string(this_job.recovery.action));
                if(this_job.option & OPT_FLG_REC_JN) {
                    if(this_job.option & OPT_FLG_REC_WS) fprintf(outFile, ", rec_after_ws = '%s'", escape_sql_string(this_job.recovery.after_workstation));
                    fprintf(outFile, ", rec_after_jn = '%s'", escape_sql_string(this_job.recovery.after_jobname));
                }
                if(this_job.option & OPT_FLG_REC_AP) fprintf(outFile, ", rec_abnd_prm = '%s'", escape_sql_string(this_job.recovery.abendprompt));
            }
            fprintf(outFile, "where workstation = '%s' and jobname = '%s';\n", (this_job.option & OPT_FLG_WS)? escape_sql_string(this_job.workstation): defaultws, escape_sql_string(this_job.jobname));
            break;
        case PRINT_FMT_CSV1: /* Comma Separated Value (CSV) Format */
            fprintf(outFile, "%s,%s,%s,%c,%s,%s,%s,%c,%s,%s,%s,%s,%s\n",
                escape_csv_string((this_job.option & OPT_FLG_WS)? this_job.workstation: defaultws),
                escape_csv_string(this_job.jobname),
                escape_csv_string(this_job.jobfile),
                this_job.jobfiletype,
                escape_csv_string(this_job.streamlogon),
                (this_job.option & OPT_FLG_DE)? escape_csv_string(this_job.description): "",
                (this_job.option & OPT_FLG_TT)? escape_csv_string(this_job.tasktype): "",
                this_job.interactive,
                (this_job.option & OPT_FLG_RC)? escape_csv_string(this_job.rccondsucc): "",
                (this_job.option & OPT_FLG_REC)? escape_csv_string(this_job.recovery.action): "",
                (this_job.option & OPT_FLG_REC & OPT_FLG_REC_JN & OPT_FLG_REC_WS)? escape_csv_string(this_job.recovery.after_workstation): "",
                (this_job.option & OPT_FLG_REC & OPT_FLG_REC_JN)? escape_csv_string(this_job.recovery.after_jobname): "",
                (this_job.option & OPT_FLG_REC & OPT_FLG_REC_AP)? escape_csv_string(this_job.recovery.abendprompt): "");
            break;
        case PRINT_FMT_XML1: /* XML Format */
            fprintf(outFile, "\t<record>\n\t\t<workstation>%s</workstation>\n", xml_encode((this_job.option & OPT_FLG_WS)? this_job.workstation: defaultws));
            fprintf(outFile, "\t\t<jobname>%s</jobname>\n", xml_encode(this_job.jobname));
            fprintf(outFile, "\t\t<docommand>%s</docommand>\n", xml_encode(this_job.jobfile));
            fprintf(outFile, "\t\t<jobtype>%c</jobtype>\n", this_job.jobfiletype);
            fprintf(outFile, "\t\t<streamlogon>%s</streamlogon>\n", xml_encode(this_job.streamlogon));
            if(this_job.option & OPT_FLG_DE) fprintf(outFile, "\t\t<description>%s</description>\n", xml_encode(this_job.description)); else fprintf(outFile, "\t\t<description/>\n");
            if(this_job.option & OPT_FLG_TT) fprintf(outFile, "\t\t<tasktype>%s</tasktype>\n", xml_encode(this_job.tasktype)); else fprintf(outFile, "\t\t<tasktype/>\n");
            fprintf(outFile, "\t\t<interactive>%c</interactive>\n", this_job.interactive);
            if(this_job.option & OPT_FLG_RC) fprintf(outFile, "\t\t<rccondsucc>%s</rccondsucc>\n", xml_encode(this_job.rccondsucc)); else fprintf(outFile, "\t\t<rccondsucc/>\n");
            if(this_job.option & OPT_FLG_REC) fprintf(outFile, "\t\t<rec_action>%s</rec_action>\n", xml_encode(this_job.recovery.action)); else fprintf(outFile, "\t\t<rec_action/>\n");
            if(this_job.option & OPT_FLG_REC & OPT_FLG_REC_JN & OPT_FLG_REC_WS) fprintf(outFile, "\t\t<rec_after_ws>%s</rec_after_ws>\n", xml_encode(this_job.recovery.after_workstation)); else fprintf(outFile, "\t\t<rec_after_ws/>\n");
            if(this_job.option & OPT_FLG_REC & OPT_FLG_REC_JN) fprintf(outFile, "\t\t<rec_after_jn>%s</rec_after_jn>\n", xml_encode(this_job.recovery.after_jobname)); else fprintf(outFile, "\t\t<rec_after_jn/>\n");
            if(this_job.option & OPT_FLG_REC & OPT_FLG_REC_AP) fprintf(outFile, "\t\t<rec_abnd_prm>%s</rec_abnd_prm>\n\t</record>\n", xml_encode(this_job.recovery.abendprompt)); else fprintf(outFile, "\t\t<rec_abnd_prm/>\n\t</record>\n");
            break;
        default:
            break;
    }
    return;
}

void print_xml_header()
{
    if(print_format != PRINT_FMT_XML1) return;
    fprintf(outFile, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<%s>\n", tablename);
}

void print_xml_footer()
{
    if(print_format != PRINT_FMT_XML1) return;
    fprintf(outFile, "</%s>\n", tablename);
}

#ifdef TESTPARSE
void version()
{
    printf("parse_job - " PARSE_JOB_VERSION ".\n" PARSE_JOB_COPYRIGHT "\n");
    return;
}

void usage()
{
    version();
    printf("\
\nusage: parse_job [-vh] [-w defaultws] [-f format] [-o output] [-t tablename] [-e error] [-l log] job-file1 [job-file2 ...]\n\n\
\t-v --version   Display version number\n\
\t-h --help      Give this help\n\
\t-w --defaultws Default workstation where jobs are defined\n\
\t-f --format    Format of output file name (see below)\n\
\t-o --output    Output file name\n\
\t-t --tablename Table name for dumping output (if SQL format is chosen)\n\
\t-e --error     Error Log File\n\
\t-l --log       Output Log File\n\n\
Output Format:\n\
\ttxt1           TWS Composer Dump Format\n\
\ttxt2           LDIF-like Text Output Format\n\
\ttxt3           Single Line Text Output Format\n\
\tsql1           INSERT SQL Statements\n\
\tsql2           UPDATE SQL Statements\n\
\tcsv1           Comma Separated Value (CSV) Format\n\
\txml1           XML Format\n\
\n\nIf file name is specified as '" STD_INPUT "', input will be read from stdin.\n\n\
Please contact " DEFAULT_CONTACT " for bug reporting or clarification.\n");
    return;
}

int main(int argc, char *argv[])
{
    int optc;
    char *inpfile;
    struct option longopts[] = {
        {"version",   no_argument,       NULL, 'v'},
        {"help",      no_argument,       NULL, 'h'},
        {"format",    required_argument, NULL, 'f'},
        {"output",    required_argument, NULL, 'o'},
        {"defaultws", required_argument, NULL, 'w'},
        {"tablename", required_argument, NULL, 't'},
        {"error",     required_argument, NULL, 'e'},
        {"log",       required_argument, NULL, 'l'},
        {NULL,        0,                 NULL, 0}
    };

    print_format = 0;
    format = (char *)NULL;
    defaultws = (char *)NULL;
    tablename = (char *)NULL;
    output_file = (char *)NULL;
    error_file = (char *)NULL;
    log_file = (char *)NULL;
    inpfile = (char *)NULL;
    logFile = (FILE *)stdout;
    errFile = (FILE *)stderr;
    outFile = (FILE *)stdout;

    while((optc = getopt_long(argc, argv, "vhf:o:w:t:e:l:", longopts, (int *) 0)) != EOF) {
        switch (optc) {
            case 'v':
                version();
                exit(0);
                break;
            case 'h':
                usage();
                exit(0);
                break;
            case 'f':
                if(format) {
                    errmsg("%s: command-line parameter for output format already specified once as '%s'.\n", optarg, format);
                    continue;
                }
                format = strdup(optarg);
                if(!strcasecmp(format, "txt1")) print_format = PRINT_FMT_TXT1;
                else if(!strcasecmp(format, "txt2")) print_format = PRINT_FMT_TXT2;
                else if(!strcasecmp(format, "txt3")) print_format = PRINT_FMT_TXT3;
                else if(!strcasecmp(format, "sql1")) print_format = PRINT_FMT_SQL1;
                else if(!strcasecmp(format, "sql2")) print_format = PRINT_FMT_SQL2;
                else if(!strcasecmp(format, "csv1")) print_format = PRINT_FMT_CSV1;
                else if(!strcasecmp(format, "xml1")) print_format = PRINT_FMT_XML1;
                else print_format = DEFAULT_PRINT_FMT;
                break;
            case 'o':
                if(output_file) {
                    errmsg("%s: command-line parameter for output file already specified as '%s'. Ignoring.\n", optarg, output_file);
                    continue;
                }
                output_file = strdup(optarg);
                if(!(outFile = fopen(output_file, "w"))) {
                    errmsg("%s: can't open output file\n", output_file);
                    exit (1);
                }
                break;
            case 'w':
                if(defaultws) {
                    errmsg("%s: command-line parameter for default workstation already specified as '%s'. Ignoring.\n", optarg, defaultws);
                    continue;
                }
                defaultws = strdup(optarg);
                break;
            case 't':
                if(tablename) {
                    errmsg("%s: command-line parameter for default tablename already specified as '%s'. Ignoring.\n", optarg, tablename);
                    continue;
                }
                tablename = strdup(optarg);
                break;
            case 'e':
                if(error_file) {
                    errmsg("%s: command-line parameter for error log file already specified as '%s'. Ignoring.\n", optarg, error_file);
                    continue;
                }
                error_file = strdup(optarg);
                if(!(errFile = fopen(error_file, "w"))) {
                    errmsg("%s: can't open output file\n", error_file);
                    exit (1);
                }
                break;
            case 'l':
                if(log_file) {
                    errmsg("%s: command-line parameter for standard log file already specified as '%s'. Ignoring.\n", optarg, log_file);
                    continue;
                }
                log_file = strdup(optarg);
                if(!(logFile = fopen(log_file, "w"))) {
                    errmsg("%s: can't open output file\n", log_file);
                    exit (1);
                }
                break;
            default:
                usage();
                exit(0);
                break;
        }
    }

    if(!print_format) print_format = DEFAULT_PRINT_FMT;
    if(!defaultws) defaultws = defaultws;
    if(!tablename) tablename = TBL_TWS_JOB;

    init_job();
    print_xml_header();
    if (argc > 1) {
        for(;optind < argc; optind++) {
            inpfile = strdup(argv[optind]);
            if(strcmp(inpfile, "-")) {
                if(!(yyin = fopen(inpfile, "r"))) {
                    errmsg("%s: can't open input job file\n", inpfile);
                    exit (1);
                }
            } else {
                yyin = stdin;
            }
            while(yyparse());
            free(inpfile);
            fclose(yyin);
        }
    } else {
        yyin = stdin;
        while(yyparse());
    }
    print_xml_footer();
    destroy_job();

    return 0;
}
/*
--------------
    if (argc > 1) {
        yyin = fopen(argv[1], "r");
        fprintf(stderr, "info:%s:using input file '%s'\n", argv[0], argv[1]);
    } else {
        yyin = stdin;
        fprintf(stderr, "info:%s:using standard input file\n", argv[0]);
    }
    fprintf(stderr, "info:%s:starting parser...\n", argv[0]);
    init_job();
    print_format = PRINT_FMT_SQL2;
    while(yyparse());
    destroy_job();
    fprintf(stderr, "info:%s:parser ended...\n", argv[0]);
    return 0;
}
*/
#endif

